package com.coderknock.spring.boot.spring.controller;

import com.coderknock.spring.boot.elasticsearch.model.Order;
import com.coderknock.spring.boot.elasticsearch.repository.OrderRepository;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.Client;
//import org.elasticsearch.search.aggregations.AggregationBuilders;
//import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
//import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
//import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram;
//import org.elasticsearch.search.aggregations.metrics.sum.Sum;
//import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p></p>
 *
 * @author 三产
 * @version 1.0
 * @date 2017-07-03
 * @QQGroup 213732117
 * @website http://www.coderknock.com
 * @copyright Copyright 2017 拿客 coderknock.com  All rights reserved.
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    //@Autowired
    //private OrderRepository orderRepository;
    //
    //@Autowired
    //private Client client;
    //
    //@PostMapping
    //@ResponseBody
    //public Order add(@RequestBody Order order) {
    //    return orderRepository.save(order);
    //}
    //
    //@GetMapping("/summary")
    //@ResponseBody
    //public Map<DateTime, Double> orderSummary() throws Exception {
    //    SearchResponse searchResponse = client.prepareSearch("order")
    //            .addAggregation(AggregationBuilders.dateHistogram("monthly").field("createdAt").interval(DateHistogramInterval.MONTH)
    //                    .subAggregation(AggregationBuilders.sum("total").field("amount")))
    //            .execute().get();
    //    Map<DateTime, Double> result = new HashMap<>();
    //    InternalHistogram<InternalHistogram.Bucket> terms = searchResponse.getAggregations().get("monthly");
    //    for (InternalHistogram.Bucket entry : terms.getBuckets()) {
    //        DateTime keyAsDate = (DateTime) entry.getKey();
    //        Sum sum = entry.getAggregations().get("total");
    //        result.put(keyAsDate, sum.getValue());
    //    }
    //    return result;
    //}
}